//
//  ShellLiquidationView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/27.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellLiquidationView.h"
#import "ShellLiquidationViewModel.h"
#import "ShellChangeTeadePasswordViewController.h"
#import "LZBaseTableView.h"
#import "ShellLiquidationCell.h"

@interface ShellLiquidationView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellLiquidationViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;

@end
@implementation ShellLiquidationView
- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.viewModel = (ShellLiquidationViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
       
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];


    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);

        [self reloadRecordData:[x integerValue]];
    }];


    [self.tableView addHeadWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getLiquidationList:YES];
    }];

    [self.tableView addAutoNormalFooterWithRefreshingBlock:^{

        @strongify(self);
        [self.viewModel getLiquidationList:NO];
    }];
       
    self.tableView.mj_footer.hidden = YES;
    showLoadingTitle(@"", nil);
    [self.viewModel getLiquidationList:YES];


//    //确认清算
//    @weakify(self);
//    [[liquidationBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        if(selectBtn.selected){
//
//            ShellUserDetailModel *detailModel = [LZToolCache getUserInfo];
//            if(!detailModel.payPasswordFlag){
//                //没有设置交易密码
//                [LZToolView showAlertType:LZAlertViewTypeTipAlter withTitle:LZLocalizedString(@"提示") message:LZLocalizedString(@"请先设置交易密码") cancel:@"" sure:LZLocalizedString(@"确定") objectDict:@{@"textAlignment":@(NSTextAlignmentCenter)} cancelBlock:^(id  _Nullable object) {} sureBlock:^(id  _Nullable object) {
//                    ShellChangeTeadePasswordViewController *changeTeadePasswordVC = [[ShellChangeTeadePasswordViewController alloc] init];
//                    [LZTool.currentViewController.navigationController pushViewController:changeTeadePasswordVC animated:YES];
//
//                }];
//                return;
//            }
//            [LZToolView showAlertType:LZAlertViewTypeTradePassword withTitle:LZLocalizedString(@"交易密码") message:LZLocalizedString(@"请输入交易密码") cancel:LZLocalizedString(@"取消") sure:LZLocalizedString(@"确定") objectDict:@{} cancelBlock:^(id  _Nullable object) { } sureBlock:^(id  _Nullable object) {
//                @strongify(self);
//                //发送请求 清算
//                [self.viewModel liquidationPassword:object];
//            }];
//
//        }
//    }];
//
//    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
//        @strongify(self);
//
//        tipLabel.text = self.viewModel.liquidationModel.textDec;
//        CGFloat tipHeight = [tipLabel.text lz_textHeightWithFontSize:tipLabel.font withMaxWidth:K_SCREENWIDTH -60];
//        [view mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(tipHeight + 40);
//        }];
//        [tipLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(tipHeight);
//        }];
//
//
//    }];
//
    
}

- (void)reloadRecordData:(NSInteger)type{
    dismiss(nil);
    if(type > 4)return;
    [self.tableView reloadData];
    [self.tableView dismissEmptyView];
    self.tableView.mj_header.state  = MJRefreshStateIdle;
    self.tableView.mj_footer.hidden = NO;
    self.tableView.mj_footer.state  = MJRefreshStateIdle;
    switch (type) {
       case 0:{
           self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
       }
           break;
       default:
           break;
    }
    
    if(self.viewModel.dataArray.count == 0){
        self.tableView.mj_footer.hidden = YES;
        [self.tableView tableViewDisplayWithImgName:@"pic_nodata" message:LZLocalizedString(@"暂无数据") contentoffsetY:109 showEmptyView:YES];
        
    }
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShellLiquidationCell *liquidationCell = [tableView dequeueReusableCellWithIdentifier:@"ShellLiquidationCell"];
    if(self.viewModel.dataArray.count > indexPath.row){
        liquidationCell.viewModel = self.viewModel;
        liquidationCell.modelObject = self.viewModel.dataArray[indexPath.row];
    }
    return liquidationCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    CGFloat tipHeight = [self.viewModel.liquidationModel.descText?:@"" lz_textHeightWithFontSize:k_textFont(13) withMaxWidth:K_SCREENWIDTH -30];
    return tipHeight + 20;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    if(self.viewModel.dataArray.count == 0){
        return [UIView new];
    }
    
    
    CGFloat tipHeight = [self.viewModel.liquidationModel.descText?:@"" lz_textHeightWithFontSize:k_textFont(13) withMaxWidth:K_SCREENWIDTH -30];
    UIView *sectionView = [[UIView alloc] init];
    sectionView.frame = CGRectMake(0, 0, K_SCREENWIDTH, tipHeight + 20);
    
    
    UILabel *tipLabel = [UILabel lz_labelWithText:self.viewModel.liquidationModel.descText fontSize:k_textFont(13) color:UIColorHex(0x678DA8)];
    [sectionView addSubview:tipLabel];
    tipLabel.text = self.viewModel.liquidationModel.descText;
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(tipHeight);
    }];
    return sectionView;
    
}


#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[ShellLiquidationCell class] forCellReuseIdentifier:@"ShellLiquidationCell"];
        _tableView.rowHeight = 70;
    }
    return _tableView;
}

@end
